package com.anywhere.freightman.system.service.impl;

import com.anywhere.freightman.common.BaseMapper;
import com.anywhere.freightman.common.BaseService;
import com.anywhere.freightman.common.entity.FileEntity;
import com.anywhere.freightman.common.ex.CustomException;
import com.anywhere.freightman.system.dao.FileDao;
import com.anywhere.freightman.system.model.FileModel;
import com.anywhere.freightman.system.service.IFileService;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsServerException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.io.IOException;

@Service
@Transactional
public class FileService extends BaseService<FileModel> implements IFileService {

    @Autowired
    private FileDao fileDao;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Override
    public FileModel upload(MultipartFile file) {
        try {
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),
                    FilenameUtils.getExtension(file.getOriginalFilename()), null);
            FileModel model = new FileModel();
            model.setFileName(file.getOriginalFilename());
            model.setGroup(storePath.getGroup());
            model.setPath(storePath.getPath());
            model.setUploader("TODO");
            insert(model);
            return model;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void delete(Long id) {
        FileModel model = getById(id);
        if (model == null) {
            throw new CustomException("文件不存在");
        }
        delete(model);
    }

    @Override
    public void delete(FileModel model) {
        super.delete(model);
        deleteFile(model.getGroup(), model.getPath());
    }

    @Override
    public void deleteFile(String path) {
        fastFileStorageClient.deleteFile(path);
    }

    @Override
    public void deleteFile(String group, String path) {
        fastFileStorageClient.deleteFile(group, path);
    }

    @Override
    public FileEntity downloadFile(Long id) {
        FileModel model = getById(id);
        if (model == null) {
            throw new CustomException("文件不存在");
        }

        FileEntity entity = new FileEntity();
        BeanUtils.copyProperties(model, entity);
        byte[] bytes = downloadFile(model.getGroup(), model.getPath());
        entity.setBytes(bytes);
        return entity;
    }

    @Override
    public byte[] downloadFile(String groupName, String path) {
        try {
            return fastFileStorageClient.downloadFile(groupName, path, new DownloadByteArray());
        } catch (FdfsServerException ex) {
            throw new CustomException(ex.getMessage());
        }

    }

    @Override
    public BaseMapper<FileModel> getBaseDao() {
        return fileDao;
    }

    @Override
    public Example getExample(FileModel entity) {
        Weekend<FileModel> weekend = Weekend.of(FileModel.class);
        WeekendCriteria<FileModel, Object> criteria = weekend.weekendCriteria();

        if (entity == null) {
            return weekend;
        }

        return weekend;
    }
}
